﻿using System;
using System.ComponentModel;
using System.Reflection;

namespace Td.Additional.ComponentModel
{
    /// <summary>
    /// Abstract implementation of <c>INotifyPropertyChanged</c> interface.
    /// </summary>
    public abstract class NotifyPropertyChanged : INotifyPropertyChanged
    {
        #region Properties

        private Boolean _Changed;

        /// <summary>
        /// Gets or sets a value indicating whether this instance is changed.
        /// </summary>
        /// <value>
        /// 	<c>true</c> if this instance is changed; otherwise, <c>false</c>.
        /// </value>
        public Boolean IsChanged
        {
            get { return _Changed; }
            set { _Changed = value; }
        }

        #endregion

        #region INotifyPropertyChanged Member

        /// <summary>
        /// Occurs when a property value changes.
        /// </summary>
        public event PropertyChangedEventHandler PropertyChanged;

        /// <summary>
        /// Called when property changed.
        /// </summary>
        /// <param name="name">The name.</param>
        protected void OnPropertyChanged(string name)
        {
            _Changed = true;
            PropertyChangedEventHandler tempHandler = PropertyChanged;
            if (tempHandler != null)
                tempHandler(this, new PropertyChangedEventArgs(name));
        }

        /// <summary>
        /// Called when property changed.
        /// <example>Example to call:
        /// <code>OnPropertyChanged(MethodBase.GetCurrentMethod());</code></example>
        /// </summary>
        /// <param name="methodBase">The method base.</param>
        protected void OnPropertyChanged(MethodBase methodBase)
        {
            _Changed = true;
            PropertyChangedEventHandler tempHandler = PropertyChanged;
            if (tempHandler != null)
                tempHandler(this, new PropertyChangedEventArgs(methodBase.Name.Substring(4)));
        }

        #endregion
    }
}